<template>
	<view>
		<view class="address-top">
			<text class="add-address" @tap="adddizhi()">添加</text>
		</view>
		<image src="../../static/images/noshuju.png" class="no-data" v-if="noshuju"></image>
		<view class="address-list" v-for="(item,indx) in list" :key="index" @tap="setdefault" :data-id="item.id">
			<view class="backgroud" >
				<view class="address-name">
					<text style="margin-right: 43rpx;">{{item.name}}</text>
					<text>{{item.phone}}</text>
				</view>
				<view class="address-info">
					{{item.province_name}} {{item.city_name}} {{item.county_name}} {{item.address}}
				</view>
				<view class="address-action">
					<radio :checked="item.default==1" style="transform:scale(0.6)" color="#38D180" ></radio>
					<text style="color: #38D180;" v-if="item.default==1">默认</text>
					<text  v-if="item.default==0">默认</text>
					<text class="shanchu"  @tap.stop="del" :data-id="item.id">删除</text>
					<text class="bianji" @tap.stop="editor" :data-id="item.id">编辑</text>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import {getlogin} from "@/static/js/login.js";
	import httpPath from '@/static/js/path.js';
	export default {
			data() {
				return {
					list:[],
					wxinfo_id:"",
					userinfo:{},
					noshuju:false,
					my:""
				}
			},
			onShow() {
				this.getdata();
			},
			onLoad(option) {
				if(option.my){
					this.my=option.my;
				}
				console.log(option.my)
			   this.userinfo = getlogin()
			   this.wxinfo_id = this.userinfo.wxinfo_id;
	           this.getdata();
			},
			methods: {
				/**
				 * @param {Object} e删除地址
				 */
				del(e){
					let id = e.currentTarget.dataset.id
					uni.request({
						url: httpPath.filePath+'/index/address/delete',
						method:'POST',
						header:{'content-type':'application/x-www-form-urlencoded'},
						data: {
							id:id,	
						},
						success: (res) => {
							if(res.data.code==200){
								this.getdata();
							}
						}
									
					});
				},
				/**
				 * 设为默认
				 */
				setdefault(e){
					let id = e.currentTarget.dataset.id
					uni.request({
						url: httpPath.filePath+'/index/address/edit',
						method:'POST',
						header:{'content-type':'application/x-www-form-urlencoded'},
						data: {
							id:id,
							wxinfo_id:this.wxinfo_id
						},
						success: (res) => {
							if(res.data.code==200){
								this.getdata();
								if(this.my!="my"){
									uni.navigateBack()
								}
								
							}
							
						}
									
					});
				},
				/**
				 * 前往修改页面
				 */
				editor(e){
					uni.navigateTo({
						url:"addaddress?id="+e.currentTarget.dataset.id
					})
				},
				/**
				 * 获取地址列表
				 */
				getdata(){
					uni.request({
						url: httpPath.filePath+'/index/address/index',
						method:'POST',
						header:{'content-type':'application/x-www-form-urlencoded'},
						data: {
							wxinfo_id:this.wxinfo_id
						},
						success: (res) => {
							if(res.data.code==200){
								if(res.data.data){
									this.list=res.data.data;
									if(this.list.length==0){
										this.noshuju=true;
									}else{
										this.noshuju=false;
									}
								}
							}
							
						}
									
					});
				},
				/**
				 * 前往添加地址
				 */
	            adddizhi(){
					uni.navigateTo({
						url:"addaddress"
					})
				}
			}
		}
</script>

<style>
	page{background: #f9f9f9;}
	.address-top{height: 87rpx;width: 100%;background: #333;}
	.add-address{font-size: 30rpx;color: #FFFFFF;font-weight: bold;float: right;display: block;padding-left: 30rpx;background: url(../../static/images/add-icon.png)no-repeat;background-size: 18rpx;background-position: 0rpx 37rpx;margin-right: 34rpx;height: 87rpx;line-height: 87rpx;}
    .address-list{height: 230rpx;width: 100%;background: url(../../static/images/border.png) no-repeat bottom;background-size: 100%;box-sizing: border-box;margin-bottom: 20rpx;}
	.address-name{font-size: 30rpx;color: #333333;font-weight: bold;line-height: 50rpx;}
	.address-info{font-size: 28rpx;color: #666666;line-height: 70rpx;}
	.address-action{height: 60rpx;font-size: 28rpx;position: relative;padding-left: 55rpx;}
	.address-action radio{position: absolute;top: -6rpx;left: -5rpx;}
	.address-action .bianji{float: right;display: inline-block;padding-left: 50rpx;background: url(../../static/images/address-editor.png) no-repeat;background-size: 30rpx;background-position: 10rpx 5rpx;}
	.address-action .shanchu{float: right;display: inline-block;padding-left: 50rpx;background: url(../../static/images/addres-del.png) no-repeat ;background-size: 25rpx;background-position: 10rpx 5rpx;margin-left: 30rpx;}
    .backgroud{background: #fff;box-sizing:border-box;padding: 32rpx;height:222rpx ;}
</style>
